package com.hadoop.mr.mapper;

import com.hadoop.mr.writable.ScoreWritable;
import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
 * Created by Administrator on 2019/7/11.
 */
public class ScoreMapper extends Mapper<LongWritable, Text, Text, ScoreWritable> {

    private static Logger logger = LoggerFactory.getLogger(ScoreMapper.class);

    @Override
    protected void map(LongWritable key, Text value, Context context)
            throws IOException, InterruptedException {
        // 将一行的内容转成 string
        try {
            String line = value.toString();
            // 用空格切割字段
            String[] fileds = line.split(";");
            String goodsName = fileds[0];
            String score = fileds[3];
            String goodsType = fileds[7];
            context.write(new Text(goodsName), new ScoreWritable(goodsName, goodsType, score, score, score, new Integer(1), Double.valueOf(score)));
        } catch (Exception e) {
            logger.error("", e);
        }
    }
}
